package com.ruoyi.finance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.finance.domain.DTO.KltPurchaseDetailStockDTO;
import com.ruoyi.finance.domain.DTO.KltSalesDetailStockDTO;
import com.ruoyi.finance.domain.KltPurchaseDetail;
import com.ruoyi.finance.domain.KltStock;
import com.ruoyi.finance.mapper.KltSalesDetailDao;
import com.ruoyi.finance.domain.KltSalesDetail;
import com.ruoyi.finance.mapper.KltStockDao;
import com.ruoyi.finance.service.KltSalesDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (KltSalesDetail)表服务实现类
 *
 * @author makejava
 * @since 2024-12-19 12:18:03
 */
@Service("kltSalesDetailService")
public class KltSalesDetailServiceImpl extends ServiceImpl<KltSalesDetailDao, KltSalesDetail> implements KltSalesDetailService {

    @Resource
    private KltStockDao kltStockDao;
    @Resource
    private KltSalesDetailDao kltSalesDetailDao;
    @Override
    public Boolean orderDetailInsertStock(KltSalesDetail kltSalesDetail) {
        boolean save = this.save(kltSalesDetail);
        KltStock kltStock = kltStockDao.selectById(kltSalesDetail.getStockId());
//       减库存
        kltStock.setStockQuantity(kltStock.getStockQuantity() - kltSalesDetail.getSalesQuantity ());
        int i = kltStockDao.updateById(kltStock);
        if(save&&i>0){
            return true;
        }else {
            new Throwable("库存更新失败");
        }
        return null;
    }

    @Override
    public List<KltSalesDetailStockDTO> getAllKltPurchaseDetailStockList(KltSalesDetail kltSalesDetail) {
        List<KltSalesDetailStockDTO> allKltPurchaseDetailStockList = kltSalesDetailDao.getAllKltPurchaseDetailStockList(kltSalesDetail);
        return allKltPurchaseDetailStockList;
    }
}

